/**
 * 给表单验证用的桥梁
 */
export interface IDomBridge {
  values: any;
  rules: any;

  //把表单dom上的值映射到service上
  set(key: string, value: any): void;

  //注册校验函数
  registerValidate(validate: () => boolean): void;

  //数据校验
  validate(): boolean;
}

export class DomBridge implements IDomBridge {
  private _validate: (() => boolean) | null = null;
  private _rules: any = {};
  public values: any = {};

  public set(key: string, value: any): void {
    if(this.values[key]){
      console.warn(`${key}的值已经存在, 请确认你想覆盖之前的值, 旧值: ${this.values[key]}`)
    }
    this.values[key] = value;
  };

  registerValidate(validate: () => boolean): void {
      this._validate = validate;
  };

  validate(): boolean {
      if(!this._validate){
        console.warn('验证函数不存在, 请先注册验证函数!!');
        return true;
      }
      return this._validate();
  };

  set rules(rules: any) {
    this._rules = rules;
  }

  get rules() {
    return this._rules;
  }
}